package com.jjx.trainingapi.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.jjx.trainingapi.common.BaseColumns;
import com.jjx.trainingapi.util.LoginManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author LoveHuahua
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.getOriginalObject() instanceof BaseColumns) {
            log.info("start insert fill ....");
            this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
            this.strictInsertFill(metaObject, "createBy", LoginManager::getUserId, Integer.class);
            this.strictUpdateFill(metaObject, "lastUpdateTime", LocalDateTime::now, LocalDateTime.class);
            this.strictInsertFill(metaObject, "lastUpdateBy", LoginManager::getUserId, Integer.class);
        }


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.getOriginalObject() instanceof BaseColumns) {
            log.info("start update fill ....");
            this.strictUpdateFill(metaObject, "lastUpdateTime", LocalDateTime::now, LocalDateTime.class);
            this.strictInsertFill(metaObject, "lastUpdateBy", LoginManager::getUserId, Integer.class);
        }

    }
}
